# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Copied from https://github.com/google/iree/blob/main/build_tools/cmake/iree_cc_library.cmake
# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include_guard(GLOBAL)

include(CMakeParseArguments)
include(cg_macros)

# cg_py_library()
#
# CMake function to imitate Bazel's py_library rule.
#
# Parameters:
# NAME: name of target (see Note)
# SRCS: List of source files for the library
# GENERATED_SRCS: List of source files for the library that are generated by other targets
# DATA: List of other targets and files required for this binary
# DEPS: List of other libraries to be linked in to the binary targets
# TESTONLY: When added, this target will only be built if user passes -DCOMPILER_GYM_BUILD_TESTS=ON to CMake.
#
function(cg_py_library)
    cmake_parse_arguments(
        _RULE
        "PUBLIC;TESTONLY"
        "NAME"
        "SRCS;GENERATED_SRCS;DATA;DEPS"
        ${ARGN}
    )

    if(_RULE_TESTONLY AND NOT COMPILER_GYM_BUILD_TESTS)
        return()
    endif()

    # TODO(boian): remove this renaming when call sites do not include ":" in target dependency names
    rename_bazel_targets(_RULE_DEPS "${_RULE_DEPS}")
    cg_target_outputs(TARGETS ${_RULE_DEPS} RESULT _DEPS_OUTPUTS)

    # Prefix the library with the package name, so we get: cg_package_name.
    rename_bazel_targets(_NAME "${_RULE_NAME}")

    unset(_BIN_PATHS)
    # Symlink each file as its own target.
    foreach(_SRC_FILE ${_RULE_SRCS})
        if(IS_ABSOLUTE _SRC_FILE)
            message(FATAL_ERROR "Absolute path for SRCS not allowed.")
        endif()

        # _SRC_FILE could have other path components in it, so we need to make a
        # directory for it. Ninja does this automatically, but make doesn't. See
        # https://github.com/google/iree/issues/6801
        set(_SRC_BIN_PATH "${CMAKE_CURRENT_BINARY_DIR}/${_SRC_FILE}")
        get_filename_component(_SRC_BIN_DIR "${_SRC_BIN_PATH}" DIRECTORY)
        add_custom_command(
            OUTPUT "${_SRC_BIN_PATH}"
            COMMAND ${CMAKE_COMMAND} -E make_directory "${_SRC_BIN_DIR}"
            COMMAND
                ${CMAKE_COMMAND} -E create_symlink
                "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}" "${_SRC_BIN_PATH}"
            DEPENDS
                "${CMAKE_CURRENT_SOURCE_DIR}/${_SRC_FILE}"
                ${_RULE_DEPS}
                ${_DEPS_OUTPUTS}
                ${_RULE_GENERATED_SRCS}
            VERBATIM
        )
        list(APPEND _BIN_PATHS "${_SRC_BIN_PATH}")
    endforeach()

    list(APPEND _BIN_PATHS ${_RULE_GENERATED_SRCS})
    set(_DEPS ${_RULE_DEPS} ${_BIN_PATHS})
    add_custom_target(${_NAME} ALL DEPENDS ${_DEPS})

    cg_add_data_dependencies(NAME ${_RULE_NAME} DATA ${_RULE_DATA})

    set_property(TARGET ${_NAME} PROPERTY OUTPUTS ${_BIN_PATHS})

    # If only one src file set the LOCATION target property to point to it.
    list(LENGTH _BIN_PATHS _BIN_PATHS_LENGTH)
    if(_BIN_PATHS_LENGTH EQUAL "1")
        set_target_properties(${_NAME} PROPERTIES LOCATION "${_BIN_PATHS}")
    endif()

    # TODO(boian): add install rules
endfunction()
